package com.example.service.impl;

import com.example.dao.ShopCartDao;
import com.example.entity.ShopCart;
import com.example.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private ShopCartDao shopCartDao;

    @Override
    public List<ShopCart> getUserShopCarts(Integer userid) {
        if (userid == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        return shopCartDao.findByUserId(userid);
    }

    @Override
    public boolean updateCartNum(Integer shopcartid, int num) {
        if (shopcartid == null || shopcartid <= 0) {
            throw new IllegalArgumentException("购物车ID无效");
        }
        if (num < 1) {
            throw new IllegalArgumentException("商品数量不能小于1");
        }
        return shopCartDao.updateNum(shopcartid, num) > 0;
    }

    @Override
    public boolean deleteCartItem(Integer shopcartid) {
        if (shopcartid == null || shopcartid <= 0) {
            throw new IllegalArgumentException("购物车ID无效");
        }
        return shopCartDao.deleteById(shopcartid) > 0;
    }

    @Override
    public boolean deleteBatch(String ids) {
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        return shopCartDao.deleteBatch(idList) > 0;
    }

    @Override
    public Double getTotalPrice(String ids) {
        if (StringUtils.isBlank(ids)) {
            return 0.0;
        }

        List<Integer> idList = Arrays.stream(ids.split(","))
                .filter(StringUtils::isNotBlank)
                .map(String::trim)
                .map(Integer::parseInt)
                .collect(Collectors.toList());

        if (idList.isEmpty()) {
            return 0.0;
        }
        return shopCartDao.calculateTotal(idList);
    }

    @Override
    public boolean addToCart(ShopCart shopCart) {
        if (shopCart == null || shopCart.getUserid() == null || shopCart.getRecipeid() == null) {
            throw new IllegalArgumentException("购物车信息不完整");
        }

        // 检查是否已存在相同商品
        ShopCart existingCart = shopCartDao.findByUserAndRecipe(
                shopCart.getUserid(),
                shopCart.getRecipeid()
        );

        if (existingCart != null) {
            // 已存在则更新数量
            existingCart.setNum(existingCart.getNum() + shopCart.getNum());
            return shopCartDao.updateNum(existingCart.getShopcartid(), existingCart.getNum()) > 0;
        } else {
            // 不存在则新增
            if (shopCart.getNum() <= 0) {
                shopCart.setNum(1); // 设置默认数量
            }
            return shopCartDao.add(shopCart) > 0;
        }
    }




}